package graphics.primitive;

import graphics.engine.*;

import java.util.ArrayList;
import java.util.Hashtable;

import javax.media.opengl.GL;

import mathematics.geometry.DiskGeometry;
import utils.Point3D;

public class Disk extends BaseStaticPrimitive 
{
	private static short slices = 32;
	
	public static Hashtable<Integer , VertexBufferObject> vbos = new Hashtable<Integer, VertexBufferObject>();
	public static int beginMode;

	public Disk(GL gl) 
	{
		super(gl);

		Disk.beginMode = GL.GL_TRIANGLE_FAN;
		
		if(!vbos.containsKey(gl.hashCode()))
		{
			vbos.put(gl.hashCode(), new VertexBufferObject());
		}
				
		this.GenerateVertices(gl);
		this.GenerateIndices(gl);
	}

	@Override
	public void GenerateVertices(GL gl)
	{
		if(Disk.vbos.get(gl.hashCode()).VertexBufferID  != 0)
		{
			return;
		}
		ArrayList<Point3D> points = new ArrayList<Point3D>();

		float angle = (float) (Math.PI * 2 / slices);
		points.add(new Point3D(0,0,0));
		for(short i = 0; i < slices + 1; ++i )
		{    
		    float x =  (float) Math.cos(angle * i) * 0.5f;
		    float y =  (float) Math.sin(angle * i) * 0.5f;
		    float z =  0.0f;

		    points.add(new Point3D(x,y,z));
		}
		
		this.geometry = new DiskGeometry(points);
		Disk.vbos.get(gl.hashCode()).GenerateVertexBuffer(gl, points);
	}

	@Override
	public void GenerateIndices(GL gl) 
	{		
		if(Disk.vbos.get(gl.hashCode()).IndexBufferID  != 0)
		{
			return;
		}
		short[] indices = new short[slices  + 2];
		for(int i = 0; i < slices  + 1; i++)
		{
			indices[i] = (short)i;
		}
		indices[slices  + 1] = 1;

		Disk.vbos.get(gl.hashCode()).GenerateIndexBuffer(gl, indices);
	}
}
